<?php
/**
 * Joomla! 1.5 component jhtraq
 *
 * @version $Id: view.html.php 2010-03-09 08:28:06 svn $
 * @author JHShop
 * @package Joomla
 * @subpackage jhtraq
 * @license GNU/GPL
 *
 * Traq type system for Joomla
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport( 'joomla.application.component.view');
class JhtraqViewVersions extends JView {
    function display($tpl = null) {

        $task = JRequest::getCmd('task', 'index');

        $tpl = $this->$task();

        parent::display($tpl);
    }

    function index() {
        $model =& JhtraqHelper::getModel('jhtraq');
        $this->versions =  $model->getVersions();
        $this->projects = $model->getProjectList();
        $this->search = JRequest::getVar('search');
        $this->project = JRequest::getVar('project');
        $this->pagination = $model->getPagination();

    }

    function add() {
        $model =& JhtraqHelper::getModel('jhtraq');
        $this->version = $model->getVersion(0);
        $this->projects = $model->getProjectList();
        return 'form';
    }

    function edit() {
        $cid = JRequest::getVar( 'cid', array(), '', 'array' );
        $model =& JhtraqHelper::getModel('jhtraq');
        $this->version = $model->getVersion($cid[0]);
        $this->projects = $model->getProjectList();
        return 'form';
    }

    function save() {
        $detail = $_POST;
        $model =& JhtraqHelper::getModel('jhtraq');
        $model->saveVersion($detail);
        $mainframe = JFactory::getApplication();
        $mainframe->redirect("index.php?option=com_jhtraq&view=versions", "Versions Updated");
    }


    function remove() {
        $model =& JhtraqHelper::getModel('jhtraq');
        $model->removeVersion($_POST['cid']);
        $mainframe = JFactory::getApplication();
        $mainframe->redirect("index.php?option=com_jhtraq&view=versions", "Versions Deleted");
    }

    /**
     * cancel editing a record
     * @return void
     */
    function cancel() {
        $mainframe = JFactory::getApplication();
        $mainframe->redirect("index.php?option=com_jhtraq&view=versions", JText::_('COM_JHTRAQ_OPERATION_CANCELLED'));
    }

    function publish() {
        $cid = JRequest::getVar( 'cid', array(), '', 'array' );
        $model =& JhtraqHelper::getModel('jhtraq');
        $this->version = $model->getVersion($cid[0]);
        $project_id = $this->version->project_id;

        //k remove all defaults!!!
        $db =&JFactory::getDBO();
        $db->setQuery("Update #__jhtraq_versions set latest='0' where project_id='".(int)$project_id."'");
        $db->query();

        $db->setQuery("Update #__jhtraq_versions set latest='1' where id='".(int)$cid[0]."'");
        $db->query();

        $mainframe = JFactory::getApplication();
        $mainframe->redirect("index.php?option=com_jhtraq&view=versions", JText::_('COM_JHTRAQ_OPERATION_SUCCESSFULL'));

        exit;
    }

    function unpublish() {
        $cid = JRequest::getVar( 'cid', array(), '', 'array' );

        $db =&JFactory::getDBO();
        $db->setQuery("Update #__jhtraq_versions set latest='0' where id='".(int)$cid[0]."'");
        $db->query();

        $mainframe = JFactory::getApplication();
        $mainframe->redirect("index.php?option=com_jhtraq&view=versions", JText::_('COM_JHTRAQ_OPERATION_SUCCESSFULL'));

        exit;
    }


}
?>